package common.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;


import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by yinzhiqun on 2015/8/27.
 *
 */
public class PlatformUtil {


//    public static boolean isRunningForeground (Context context){
//        ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
//        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
//        String currentPackageName = cn.getPackageName();
//        if(currentPackageName!=null && currentPackageName.equals(context.getPackageName())){
//            return true ;
//        }
//
//        return false ;
//    }

    public static boolean isRunningForeground (Context context){
        //锁屏界面时，也会判定为在前台。。。

        ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        if (processInfos!=null){
            String packageName = context.getPackageName();
            for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
                if (processInfo.processName.equals(packageName)&&
                        processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
                    return true;
                }
            }
        }
        return false ;
    }

    /**
     * 判断手机是否有安装此应用
     * params1 context--上下文
     * params2  packagename--应用的包名
     * return Bollean 有返回true，没有返回false
     * */
    public static boolean isAvilible(Context context, String packageName){
//        List<PackageInfo> pinfo = context.getPackageManager().getInstalledPackages(0);//获取所有已安装程序的包信息
//        //从pinfo中将包名字逐一取出，压入pName list中
//        if(pinfo != null){
//            for(int i = 0; i < pinfo.size(); i++){
//                String pn = pinfo.get(i).packageName;
//                if (packageName.equals(pn)){
//                    return true;
//                }
//            }
//        }
//        return false;

        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null) {
            return false;
        } else {
            return true;
        }
    }

    private static int windowWidth;
    private static int windowHeight;
    private static int statusBarHeight;

    public static int windowWidth(Activity context){
        if (windowWidth==0) {
            getWindowSize(context);
        }
        return windowWidth;
    }

    public static int windowHeight(Activity context){
        if (windowHeight==0){
            getWindowSize(context);
        }
        return windowHeight;
    }

    private static void getWindowSize(Activity context){
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        windowWidth = dm.widthPixels;
        windowHeight = dm.heightPixels;
    }

    public static int statusBarHeight(Activity context){
        if (statusBarHeight==0){
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object obj = c.newInstance();
                Field field = c.getField("status_bar_height");
                int x = Integer.parseInt(field.get(obj).toString());
                statusBarHeight = context.getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();

                Rect outRect = new Rect();
                context.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
                statusBarHeight = outRect.top;
            }
        }
        return statusBarHeight;
    }


    public static String getMetaValue(Context context, String metaKey) {
        Bundle metaData = null;
        String apiKey = null;
        if (context == null || metaKey == null) {
            return null;
        }
        try {
            ApplicationInfo ai = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            if (null != ai) {
                metaData = ai.metaData;
            }
            if (null != metaData) {
                apiKey = metaData.getString(metaKey);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return apiKey;
    }
}
